package Game;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;

public class Chessboard extends JPanel implements Config, MouseListener {
    private int[][] chesses;
    private int index = 0;
    private boolean chessable = false;

    public Chessboard() {
        chesses = new int[ROWS][COLUMNS];
        addMouseListener(this);
        setLayout(null);
        setVisible(true);
    }

    public void paint(Graphics g) {
        super.paint(g);
        g.setFont(new Font("Times New Roman", Font.PLAIN, CHESS_SIZE / 2));
        g.setColor(Color.ORANGE);
        g.fillRect(0, 0, 2 * X0 + (COLUMNS - 1) * SIZE, 2 * Y0 + (ROWS - 1) * SIZE);
        g.setColor(Color.BLACK);
        g.drawRect(X0, Y0, SIZE * (COLUMNS - 1), SIZE * (ROWS - 1));
        for (int row_id = 1; row_id < ROWS; row_id++) {
            g.drawLine(X0, Y0 + row_id * SIZE, X0 + (COLUMNS - 1) * SIZE, Y0 + row_id * SIZE);
        }
        for (int column_id = 1; column_id < COLUMNS; column_id++) {
            g.drawLine(X0 + column_id * SIZE, Y0, X0 + column_id * SIZE, Y0 + (ROWS - 1) * SIZE);
        }
        int row_index = 0, column_index = 0;
        for (int[] rows : chesses) {
            for (int cell : rows) {
                if (cell != 0) {
                    chooseColor(g, cell);
                    g.fillOval(X0 + column_index * SIZE - CHESS_SIZE / 2, Y0 + row_index * SIZE - CHESS_SIZE / 2, CHESS_SIZE, CHESS_SIZE);
                    g.setColor(Color.BLACK);
                    g.drawOval(X0 + column_index * SIZE - CHESS_SIZE / 2, Y0 + row_index * SIZE - CHESS_SIZE / 2, CHESS_SIZE, CHESS_SIZE);
                    chooseColor(g, cell + 1);
                    g.drawString(String.format("%03d", cell), X0 + column_index * SIZE - (int) (CHESS_SIZE * 0.35), Y0 + row_index * SIZE + (int) (CHESS_SIZE * 0.15));
                }
                column_index += 1;
            }
            row_index += 1;
            column_index = 0;
        }
    }

    private void chooseColor(Graphics g, int index) {
        if (index % 2 == 0) {
            g.setColor(Color.WHITE);
        } else if (index % 2 == 1) {
            g.setColor(Color.BLACK);
        }
    }

    public void addChess(int row_index, int column_index) {
        int result;
        if (chesses[row_index][column_index] == 0) {
            index++;
            chesses[row_index][column_index] = index;
            result = checkWinner();
            if (result != 0) {
                System.out.println(result);
                chessable = false;
            }
        }
    }

    public void restart() {
        index = 0;
        chessable = false;
        chesses = new int[ROWS][COLUMNS];
    }

    public void setChessable(boolean ca) {
        chessable = ca;
    }

    public int checkWinner() {
        int result = 0;
        boolean playerWinned = false;
        circle:
        for (int row_index = 0; row_index < ROWS; row_index++) {
            for (int column_index = 0; column_index < COLUMNS; column_index++) {
                if (column_index >= 4 && row_index <= ROWS - 5) {
                    for (int player = 1; player < 3; player++) {
                        playerWinned = true;
                        for (int index = 0; index < 5; index++) {
                            result = chesses[row_index + index][column_index - index];
                            if (result % 2 != player || result == 0) {
                                playerWinned = false;
                                break;
                            }
                        }
                        if (playerWinned) {
                            break circle;
                        }
                    }
                }
                if (column_index <= COLUMNS - 5 && row_index <= ROWS - 5) {
                    for (int player = 1; player < 3; player++) {
                        playerWinned = true;
                        for (int index = 0; index < 5; index++) {
                            result = chesses[row_index + index][column_index + index];
                            if (result % 2 != player || result == 0) {
                                playerWinned = false;
                                break;
                            }
                        }
                        if (playerWinned) {
                            break circle;
                        }
                    }
                }
                if (row_index <= ROWS - 5) {
                    for (int player = 1; player < 3; player++) {
                        playerWinned = true;
                        for (int index = 0; index < 5; index++) {
                            result = chesses[row_index + index][column_index];
                            if (result % 2 != player || result == 0) {
                                playerWinned = false;
                                break;
                            }
                        }
                        if (playerWinned) {
                            break circle;
                        }
                    }
                }
                if (column_index <= COLUMNS - 5) {
                    for (int player = 1; player < 3; player++) {
                        playerWinned = true;
                        for (int index = 0; index < 5; index++) {
                            result = chesses[row_index][column_index + index];
                            if (result % 2 != player || result == 0) {
                                playerWinned = false;
                                break;
                            }
                        }
                        if (playerWinned) {
                            break circle;
                        }
                    }
                }
            }
        }
        if (index == ROWS * COLUMNS && !playerWinned) {
            result = 3;
        } else if (!playerWinned) {
            result = 0;
        }
        return result;
    }

    @Override
    public void mouseClicked(MouseEvent e) {

    }

    @Override
    public void mousePressed(MouseEvent e) {

    }

    @Override
    public void mouseReleased(MouseEvent e) {
        if (chessable) {
            int x = e.getX(), y = e.getY();
            System.out.println(String.format("%d %d", x, y));
            x += SIZE / 2 - X0;
            y += SIZE / 2 - Y0;
            System.out.println(String.format("%d %d", x, y));
            int column_index = x / SIZE, row_index = y / SIZE;
            System.out.println(String.format("%d %d", row_index, column_index));
            if ((x % SIZE - SIZE / 2) * (x % SIZE - SIZE / 2) + (y % SIZE - SIZE / 2) * (y % SIZE - SIZE / 2) <= (CHESS_SIZE / 2) * (CHESS_SIZE / 2)) {
                System.out.println("True");
                addChess(row_index, column_index);
            } else {
                System.out.println("False");
            }
            repaint();
        }
    }

    @Override
    public void mouseEntered(MouseEvent e) {

    }

    @Override
    public void mouseExited(MouseEvent e) {

    }
}
